import { defineStore } from "pinia";
import { Names } from "../../store-namespace";
import { _get, _post, _put, _delete, http } from "@/utils/axios";
import { ElNotification } from "element-plus"; //处理导航信息
import { Dictionary, DictData } from "@/api/backend/index";

export const useDictionary = defineStore(Names.dictionary, {
  state: () => {
    return {
      dictType: "", //字典项待查类型
      //字典项单个数据
      opera: "add",
      tag: <any>{}, //默认tag
      moreTags: <any>{}, //额外tags
      planstate: <any>{}, //生产计划状态
      plansuspend: <any>{}, //生产计划是否暂停状态
      planpriority: <any>{}, //生产计划优先级状态
      processType: <any>{}, //工序类型
    };
  },
  getters: {},
  actions: {
    //新增字典项
    addDictSet(data: anyObj) {
      return new Promise((resolve, reject) => {
        _post("/system/dict/data", data, "json", "")
          .then((res) => {
            console.log(res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //修改字典项
    editDictSet(data: anyObj) {
      return new Promise((resolve, reject) => {
        _put("/system/dict/data", data, "json", "")
          .then((res) => {
            console.log(res);
            resolve(res);
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //删除字典项
    deleteDictSet(data: anyObj) {
      return new Promise((resolve, reject) => {
        _delete("/system/dict/data" + `/${data.dictCode}`, {}, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    // 删除字典
    deleteDict(data: anyObj) {
      return new Promise((resolve, reject) => {
        _delete(Dictionary + `/${data.dictId}`, {}, "json", "")
          .then((res: any) => {
            console.log(res);
            resolve(res);
            ElNotification({
              type: "success",
              message: res.data.msg,
            });
          })
          .catch((err) => {
            console.log(err);
            reject(err);
          });
      });
    },
    //查询字典项
    getDictInfo(type: string) {
      return new Promise((resolve, reject) => {
        _get(DictData + `/${type}`, "", "json", "").then((res: any) => {
          console.log("查询字典项", res);
          resolve(res.data);
        });
      });
    },
  },
});
